package cn.com.ddapp.ddm.ui.mine;

import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import butterknife.BindView;
import butterknife.OnClick;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.Entity;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.util.TimeCount;
import cn.com.ddapp.ddm.util.ToolFor9Ge;

public class EmailActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.titletext)
    TextView titletext;
    @BindView(R.id.et_email)
    EditText etEmail;
    @BindView(R.id.et_verify)
    EditText etVerify;
    @BindView(R.id.bind_email)
    Button bindEmail;
    @BindView(R.id.getcode_email)
    TextView getCode;
    //运行线程
    boolean run = true;
    Handler handler;
    TimeCount timeCount;

    @Override
    public void initView() {
        titletext.setText(R.string.bind_email);
        /*handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what ==1){
                    if (msg.arg1>=0&&msg.arg2==1&&run){
                        getCode.setText(msg.arg1+"s后重新获取");
                    }else{
                        //重新获取验证码
                        etVerify.setText("");
                        getCode.setText(R.string.again);//重新获取验证码
                        getCode.setEnabled(true);
                        getCode.setTextColor(getResources().getColor(R.color.color_orange));
                    }
                }
            }
        };*/
    }

    @Override
    public void initData() {
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_email;
    }

    @Override
    protected String getFragmentTag() {
        return null;
    }

    @OnClick({R.id.back,R.id.bind_email,R.id.getcode_email})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.back :
                finish();
                break;
            case R.id.bind_email :
                String userId = SPUtils.getStringData(this, SPUtils.USERID);
                String email = etEmail.getText().toString();
                if ("".equals(email)){
                    BaseApplication.getIntstance().showToast("请输入邮箱", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                }else if ("".equals(etVerify.getText().toString())){
                    BaseApplication.getIntstance().showToast("请输入验证码", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                }else {
                    OkUtilCallback callback = new OkUtilCallback() {
                        @Override
                        public void onMyResponse(String response, int id) {
                            Gson gson = new Gson();
                            Entity entity = gson.fromJson(response, Entity.class);
                            if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult())) && entity.getLogin()) {
                                BaseApplication.getIntstance().showToast("请检查网络", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                            } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                                BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                            } else if ((AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                                BaseApplication.getIntstance().showToast("绑定成功", Toast.LENGTH_SHORT, 0, Gravity.TOP);
                                finish();
                            }
                        }
                    };
                    OKUtilAPI.post(OKUtilAPI.updateEmail,"userId="+userId+"&email="+email+"&randCode="+etVerify.getText().toString(),callback);
                }
                break;
            case R.id.getcode_email :
                if ("".equals(etEmail.getText().toString())){
                    BaseApplication.getIntstance().showToast("请输入邮箱", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                }else {
                    /*run = true;
                    getCode.setText("60秒后重新获取");
                    //设置按钮不可点击并为灰色
                    getCode.setEnabled(false);
                    getCode.setTextColor(getResources().getColor(R.color.divider_line));
                    Thread thread = new Thread(new Runnable() {
                        @Override
                        public synchronized void run() {
                            int i = 59;
                            while (run){
                                SystemClock.sleep(1000);
                                Message message = Message.obtain();
                                message.what = 1;
                                message.arg1 = i;
                                message.arg2 = 1;
                                handler.sendMessage(message);
                                i--;
                                if (i<0){
                                    run = false;
                                }
                            }
                        }
                    });
                    thread.start();*/
                    OkUtilCallback callback = new OkUtilCallback() {
                        @Override
                        public void onMyResponse(String response, int id) {
                            Gson gson = new Gson();
                            Entity entity = gson.fromJson(response, Entity.class);
                            if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult())) && entity.getLogin()) {
                                BaseApplication.getIntstance().showToast("请检查网络", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                            } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                                BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                            } else if ((AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                                BaseApplication.getIntstance().showToast("验证码已发送", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                                timeCount.start();
                            }
                        }
                    };
                    OKUtilAPI.post(OKUtilAPI.sendVerifyCode, "key=" + etEmail.getText().toString(), callback);
                }
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        timeCount = new TimeCount(60000, 1000, getCode);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        timeCount.cancel();
    }
}
